package com.atguigu.assionexcel.UnSafeCollection;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 问题： Set set = new HashSet(); 多线程下出现ConcurrentModificationException异常
 * 出现原因：add 方法没有加锁
 * 解决方法：换一个集合类
 *        1、Set<String> set = new HashSet<>(); 默认
 *        2、Set<String> set = Collections.synchronizedSet(new HashSet<>());
 *        3、Set<String> set = new CopyOnWriteArraySet();
 */
public class SetT1 {
    public static void main(String[] args) {




        Set set = new HashSet();   //推荐
        for (int i = 0; i < 30000; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0,3));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
